﻿#region Using

using System;
using System.Collections.Generic;
using System.Linq;
using Vk.Methods.Audios;
using Vk.Methods.Common.AttachmentTypes;
using Vk.Methods.Docs;
using Vk.Methods.Notes;
using Vk.Methods.Pages;
using Vk.Methods.Photos;
using Vk.Methods.Polls;
using Vk.Methods.Wall;

#endregion

namespace Vk.Methods.Common
{
    /// <summary>
    ///     Класс для парсинга вложений
    /// </summary>
    public static class Attachments
    {
        /// <summary>
        ///     Извлекает информацию о вложении
        /// </summary>
        /// <param name="type">Тип вложения</param>
        public static AttachmentData GetAttachment(WallAttachmentType type, ApiNode node)
        {
            switch (type)
            {
                case WallAttachmentType.App:
                    {
                        var a = new AttachmentApplication
                        {
                            Id = node.Int("id"),
                            Name = node.String("name"),
                            Photo130 = node.String("photo_130"),
                            Photo604 = node.String("photo_604")
                        };
                        return a;
                    }
                case WallAttachmentType.Audio:
                    {
                        var a = new AttachmentEntity<Audio> { Entity = new Audio(node) };
                        return a;
                    }
                case WallAttachmentType.Graffiti:
                    {
                        return new AttachmentGraffiti
                        {
                            Id = node.Int("id"),
                            OwnerId = node.Int("owner_id"),
                            Photo200 = node.String("photo_200"),
                            Photo586 = node.String("photo_586")
                        };
                    }
                case WallAttachmentType.Note:
                    {
                        return new AttachmentEntity<Note> { Entity = new Note(node) };
                    }
                case WallAttachmentType.Photo:
                    {
                        return new AttachmentEntity<Photo> { Entity = new Photo(node) };
                    }
                case WallAttachmentType.PostedPhoto:
                    {
                        return new AttachmentPhoto
                        {
                            Id = node.Int("id"),
                            OwnerId = node.Int("owner_id"),
                            Photo130 = node.String("photo_130"),
                            Photo604 = node.String("photo_604")
                        };
                    }
                case WallAttachmentType.Poll:
                    {
                        return new AttachmentEntity<Poll> { Entity = new Poll(node) };
                    }
                case WallAttachmentType.Video:
                    {
                        return new AttachmentEntity<Video.Video> { Entity = new Video.Video(node) };
                    }
                case WallAttachmentType.Link:
                    {
                        return new AttachmentLink(node);
                    }
                case WallAttachmentType.Doc:
                    {
                        return new AttachmentEntity<Document> { Entity = new Document(node) };
                    }
                case WallAttachmentType.Page:
                    {
                        return new AttachmentEntity<Page> { Entity = new Page(node) };
                    }
                case WallAttachmentType.Album:
                    {
                        return new AttachmentAlbum
                        {
                            Id = node.Int("id"),
                            OwnerId = node.Int("owner_id"),
                            Size = node.Int("size"),
                            Thumb = node.Object("thumb", x => new Photo(x)),
                            Created = node.DateTime("created"),
                            Updated = node.DateTime("updated"),
                            Description = node.String("description"),
                            Title = node.String("title")
                        };
                    }
                case WallAttachmentType.PhotosList:
                    return new AttachmentEntity<List<string>>
                    {
                        Entity = node.ChildNodes.Select(x => x.String()).ToList()
                    };
                case WallAttachmentType.Market:
                    //TODO
                    throw new NotImplementedException();
                case WallAttachmentType.MarketAlbum:
                    throw new NotImplementedException();
                default:
                    throw new ArgumentOutOfRangeException(nameof(type), type, null);
            }
        }

        /// <summary>
        ///     Извлекает информацию о вложении
        /// </summary>
        /// <param name="type">Тип вложения</param>
        /// <param name="node">Элемент DOM, который сожержит информацию в XML-представлении</param>
        public static AttachmentData GetAttachment(MessageAttachmentType type, ApiNode node)
        {
            switch (type)
            {
                case MessageAttachmentType.Video:
                    {
                        return new AttachmentEntity<Video.Video> { Entity = new Video.Video(node) };
                    }
                case MessageAttachmentType.Photo:
                    {
                        return new AttachmentEntity<Photo> { Entity = new Photo(node) };
                    }
                case MessageAttachmentType.Audio:
                    {
                        return new AttachmentEntity<Audio> { Entity = new Audio(node) };
                    }
                case MessageAttachmentType.Doc:
                    {
                        return new AttachmentEntity<Document> { Entity = new Document(node) };
                    }
                case MessageAttachmentType.Wall:
                    {
                        return new AttachmentEntity<WallPost> { Entity = new WallPost(node) };
                    }
                case MessageAttachmentType.WallReply:
                    {
                        return new AttachmentEntity<Comment> { Entity = new Comment(node) };
                    }
                case MessageAttachmentType.Sticker:
                    {
                        return new AttachmentSticker(node);
                    }
                case MessageAttachmentType.Link:
                    {
                        return new AttachmentLink(node);
                    }
                case MessageAttachmentType.Gift:
                    {
                        return new AttachmentGift(node);
                    }
                case MessageAttachmentType.Market:
                    //TODO
                    throw new NotImplementedException();
                case MessageAttachmentType.MarketAlbum:
                    throw new NotImplementedException();
                default:
                    throw new ArgumentOutOfRangeException(nameof(type), type, null);
            }
        }
    }
}